import 'package:flutter/material.dart';

void main() => runApp(RouteTest2());

class RouteTest2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'RouteTest2',
      routes: {
        "second_page": (context) => SecondPage(),
        "third_page": (context) => ThirdPage(),
      },
      home: FirstPage(),
    );
  }
}

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  String _backMsg = 'xixi';
  @override
  Widget build(BuildContext context) {
    var secondArgs = {"id": 23, "x": "sdf"};

    return Scaffold(
        appBar: AppBar(
          title: Text('the first page'),
        ),
        body: Column(
          children: <Widget>[
            RaisedButton(
                onPressed: () => Navigator.pushNamed(context, "second_page",
                            arguments: secondArgs)
                        .then((backMsg) {
                      setState(() {
                        _backMsg = backMsg;
                      });
                    }),
                child: Text('去第二页')),
            Text('第二页返回的值$_backMsg'),
          ],
        ));
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Map<String, Object> msg =
        ModalRoute.of(context).settings.arguments as Map<String, Object>;
    int id = msg["id"];
    return Scaffold(
        appBar: AppBar(
          title: Text('the second page'),
        ),
        body: Column(
          children: <Widget>[
            Text('Message from first screen: $id'),
            RaisedButton(
                child: Text('back'),
                onPressed: () => Navigator.pop(context, "你大爷的"))
          ],
        ));
  }
}

class ThirdPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('the third page'),
      ),
    );
  }
}
